package base.engine;

import base.Start;
import base.common.IHandler;
import base.world.World;

import com.world.WorldMenu;

public class WorldHandler implements IHandler
{
	/**Current world*/
	public World theWorld;
	/**Next world to load*/
	public World theNextWorld;

	@Override
	public void initiate()
	{
		if (theNextWorld == null)
		{
			theNextWorld = new WorldMenu();
		}

		theWorld = theNextWorld;
		theWorld.initWorld();
	}

	@Override
	public void update()
	{
		this.updateCurrentWorld();
	}

	@Override
	public void terminate()
	{
		this.theWorld.terminateWorld();
	}

	/**Main world render*/
	public void render()
	{
		this.renderCurrentWorld();
	}

	public void updateCurrentWorld()
	{
		if (theWorld != null)
		{
			if (!theWorld.isDead)
			{
				theWorld.updateWorld();
			}
		}
		else
		{
			Start.terminateApplication();
		}
	}

	public void renderCurrentWorld()
	{
		if (theWorld != null)
		{
			if (!theWorld.isDead)
			{
				theWorld.render();
			}
		}
	}

	/**Set world to current world and dispose old world*/
	public synchronized void moveToWorld(World par1World)
	{
		if (par1World != null)
		{
			theNextWorld = par1World;
			Start.refreshApplication();
		}
	}
}
